package com.suredy.app.porject.ctrl;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.suredy.app.porject.model.Bid;
import com.suredy.app.porject.model.BidMoney;
import com.suredy.app.porject.service.BidMoneySrv;
import com.suredy.core.ctrl.BaseCtrl;
import com.suredy.core.mvc.model.MessageModel;

/**
 * 招标资金控制器
 * 
 * @author VIVID.G
 * @since 2015-12-24
 * @version v0.1
 */
@RequestMapping("/bid-money")
@Controller
public class BidMoneyCtrl extends BaseCtrl {

	@Autowired
	private BidMoneySrv bidMoneySrv;

	@RequestMapping(value = "/gave-back-money/{bidId}/{moneyId}", method = RequestMethod.POST)
	@ResponseBody
	public Object gaveBackMoney(@PathVariable String bidId, @PathVariable String moneyId) {
		if (StringUtils.isBlank(moneyId) || StringUtils.isBlank(bidId))
			return new MessageModel(false, "操作失败，无效的费用信息！", null);

		BidMoney m = new BidMoney();
		m.setId(moneyId);
		m.setBid(new Bid());
		m.getBid().setId(bidId);

		m = this.bidMoneySrv.readSingleByEntity(m);

		if (m == null || StringUtils.isBlank(m.getId()))
			return new MessageModel(false, "操作失败，无法找到对应的费用信息！", null);

		m.setGaveBack(true);

		m = this.bidMoneySrv.update(m);

		if (m == null || StringUtils.isBlank(m.getId()))
			return new MessageModel(false, "操作失败，回收资金失败！", null);

		return new MessageModel(true, "操作成功！", null);
	}

	@RequestMapping(value = "/delete/{id}", method = RequestMethod.POST)
	@ResponseBody
	public Object deleteMoney(@PathVariable String id) {
		if (StringUtils.isBlank(id))
			return new MessageModel(false, "操作失败，资金信息无效！", null);

		BidMoney money = this.bidMoneySrv.get(id);

		if (money == null)
			return new MessageModel(false, "操作失败，无法找到资金信息！", null);

		money = this.bidMoneySrv.delete(money);

		if (money == null)
			return new MessageModel(false, "操作失败，无法删除资金信息！", null);

		return new MessageModel(true, "操作成功！", null);
	}

	@RequestMapping(value = "/edit/{id}/{bidId}", method = RequestMethod.GET)
	public String editMoneyForm(@PathVariable String id, @PathVariable String bidId, Model model) {
		String view = "app/bid/bid-money-edit";

		if (StringUtils.isBlank(id) || StringUtils.isBlank(bidId)) {
			model.addAttribute("invalid", true);
			return view;
		}

		BidMoney data = this.bidMoneySrv.get(id);

		if (data == null) {
			model.addAttribute("invalid", true);
			return view;
		}

		model.addAttribute("data", data);
		model.addAttribute("bidId", bidId);

		return view;
	}

	@RequestMapping(value = "/edit", method = RequestMethod.POST)
	@ResponseBody
	public Object editMoney(BidMoney money) {
		if (money == null || StringUtils.isBlank(money.getId()) || money.getBid() == null || StringUtils.isBlank(money.getBid().getId()))
			return new MessageModel(false, "操作失败，资金信息无效！", null);

		money = this.bidMoneySrv.update(money);

		if (money == null)
			return new MessageModel(false, "操作失败，资金信息无法保存！", null);

		return new MessageModel(true, "操作成功！", null);
	}

}
